package AlienArena::Server;
use strict;
use warnings;
use lib '../..';

our $AUTOLOAD;

# 7.53
our @SERVER_CMDS = qw[
Sv
Dir
Map
Say
Set
Bind
Echo
Exec
Hang
Kick
Path
Quit
Wait
Alias
Error
Heartbeat
Cmdlist
Cvarlist
Setmaster
Startmap
Demomap
Dumpuser
Serverinfo
Serverstop
Serverrecord
Status
Killserver
Z_Stats
Unalias
];

sub AUTOLOAD {
    my $self = shift;
    my (@args) = @_;
    my $cmd = $AUTOLOAD;
    $cmd =~ s/.*://;
    if (grep /^$cmd$/,@SERVER_CMDS) {
	return unless $self->_check_console;
	# returns AlienArena::RCon::Response object
	return $self->console->send_command(join(" ", $cmd, @_));
    }
    else {
	die "Can't locate object method \"$cmd\" via package ".__PACKAGE__;
    }
    1;
}

sub Var {
    my $self = shift;
    my ($cvar, $value)  = @_;
    return unless $self->_check_console;
    my $s = (defined $value ? "$cvar $value" : $cvar);
    my $resp = $self->console->send_command($s);
    return unless $resp->ok;
    ($value) = $resp->parsed_response =~ /\S+\s+(.*)/;
    return $value;
}

sub DESTROY {}

=head1 NAME

AlienArena::Server::Commands - Methods mapping to basic AA server commands

=head1 SYNOPSIS

    use AlienArena::Server;
    ...
    svr->Set('ctf', 1) unless $svr->Var('admin') eq 'Lancelot';
    $resp = $svr->Status;
    print $resp->as_text;
    $svr->Killserver
    ...
    @available_commands = @AlienArena::Server::Commands::SERVER_CMDS;

=head1 DESCRIPTION

AlienArena::Server::Commands provides AlienArena::Server methods
corresponding to each basic server command as defined in sv_cmds.c in
the AA distribution.

The methods are the name of the command, capitalized. Method arguments
are passed as command arguments on the console 'line', separated by
spaces. C<Var()> is an exception; see below.

The methods return the server response as an
L<AlienArena::RCon::Response> object.

=head1 METHODS

=head2 E<lt>CommandE<gt>

Any AlienArena server command, capitalized. Listed in C<@AlienArena::Server::Commands::SERVER_CMDS.>

=head2 Var($cvar_name)

Returns the value of the cvar as a scalar.

=head1 AUTHOR

Mark A. Jensen, C<< <maj -at- fortinbras -dot- us> >>

=head1 LICENSE AND COPYRIGHT

Copyright 2012 Mark A. Jensen.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=cut

1;

